package org.example.campus_sys_backend.controller;

import org.example.campus_sys_backend.mapper.UserInfoMapper;
import org.example.campus_sys_backend.model.UserInfo;
import org.example.campus_sys_backend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    public UserService userService;


    @RequestMapping("/login")
    public boolean login(String loginName, String password, HttpSession session){
        if (!StringUtils.hasLength(loginName)|| !StringUtils.hasLength(password)){
            return false;
        }
        UserInfo userInfo = userService.queryUserByName(loginName);

        //验证账号密码
        if (userInfo==null){
            return false;
        }
        if (password.equals(userInfo.getPassword())){
            userInfo.setPassword("");
            session.setAttribute("userName",userInfo);
            return true;
        }
        return false;
    }
    @RequestMapping("/findid")
    public UserInfo findId(Integer id){
        if (id > 0){

            return userService.queryUserById(id);
        }
        return null;
    }
    @RequestMapping("/findname")
    public UserInfo findName(String name){
        if (name != null ){
            return userService.queryUserByName(name);
        }
        return null;
    }
    @RequestMapping("/add")
    public boolean addUser(UserInfo userInfo){
       if (userInfo!=null){
         userService.insertUser(userInfo);
           return true;
       }
       return false;
    }
    }



